home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_2 / star-1_0.tar / forms.c < prev    next >
C/C++ Source or Header  |  1991-03-22  |  4KB  |  200 lines

  1. /* forms.c -- STAR Formats
  2.  
  3. This file is part of STAR, the Saturn Macro Assembler.
  4.  
  5.    STAR is not distributed by the Free Software Foundation. Do not ask
  6. them for a copy or how to obtain new releases. Instead, send e-mail to
  7. the address below. STAR is merely covered by the GNU General Public
  8. License.
  9.  
  10. Please send your comments, ideas, and bug reports to
  11. Jan Brittenson <bson@ai.mit.edu>
  12.  
  13. */
  14.  
  15.  
  16. /* Copyright (C) 1990, 1991 Jan Brittenson.
  17.  
  18.    STAR is free software; you can redistribute it and/or modify it
  19. under the terms of the GNU General Public License as published by the
  20. Free Software Foundation; either version 1, or (at your option) any
  21. later version.
  22.  
  23.    STAR is distributed in the hope that it will be useful, but WITHOUT
  24. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  25. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  26. for more details.
  27.  
  28.    You should have received a copy of the GNU General Public License
  29. along with STAR; see the file COPYING. If not, to obtain a copy, write
  30. to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
  31. USA, or send e-mail to bson@ai.mit.edu. */
  32.  
  33.  
  34. #include <stdio.h>
  35. #include "star.h"
  36. #include "code.h"
  37.  
  38.  
  39. /* Global variables */
  40. extern errcnt, pass;
  41.  
  42.  
  43. /* Local data */
  44. char *mnam[] = {
  45.   "*UNDEFINED*",
  46.   "register",
  47.   "immediate",
  48.   "register indirect",
  49.   "ID",
  50.   "C nibble",
  51.   "P+1"};
  52.  
  53.  
  54. /* Return name of addressing mode */
  55. static char *modename(argp)
  56.   register struct astruct *argp;
  57. {
  58.   register int type;
  59.   
  60.   type = argp->type;
  61.   
  62.   /* Illegal mode? */
  63.   if(type < T_REG || type > T_ID)
  64.     return(mnam[0]);
  65.   
  66.   return(mnam[type]);
  67. }
  68.  
  69.  
  70. /* Make sure disp1 field is in selected range.
  71.  * The range should contain 0.
  72.  */
  73. void assert_imm_range(argp, lolimit, hilimit)
  74.   struct astruct *argp;
  75.   INT lolimit;
  76.   unsigned INT hilimit;
  77. {
  78.   /* Implicit isimm() */
  79.   if(argp->type != T_IMM)
  80.     {
  81.       /* Display message */
  82.       isimm(argp);
  83.  
  84.       argp->disp1 = lolimit;
  85.       return;
  86.     }
  87.  
  88.   /* Test range */
  89.   if(hilimit && lolimit &&
  90.      ((argp->disp1 > 0 && (unsigned INT) argp->disp1 > hilimit) ||
  91.       (argp->disp1 < 0 && (INT) argp->disp1 < lolimit)))
  92.     {
  93.       /* Out of range -- signal error and set value to 0 */
  94.       sgnerr("Argument value of 0x%lx out of range", /*  0x%lx to 0x%lx", */
  95.          (long) argp->disp1 /* , (long) lolimit, (long) hilimit */ );
  96.       
  97.       argp->disp1 = 0;
  98.     }
  99. }
  100.  
  101.  
  102. /* Make sure INT is in selected range */
  103. void assert_range(i, lolimit, hilimit)
  104.   INT i, lolimit;
  105.   unsigned INT hilimit;
  106. {
  107.   struct astruct a;
  108.  
  109.   a.type = T_IMM;
  110.   a.disp1 = i;
  111.  
  112.   assert_imm_range(&a, lolimit, hilimit);
  113. }
  114.  
  115.  
  116. /* Make sure argument is of mode register
  117.  * If not, make it A and signal error
  118.  */
  119. isanyreg(argp)
  120.   register struct astruct *argp;
  121. {
  122.   if(argp->type == T_REG)
  123.     return(TRUE);
  124.   
  125.   sgnerr("Must be register, not %s", modename(argp));
  126.   
  127.   argp->type = T_REG;
  128.   argp->pres = 0;
  129.   argp->reg  = 0;
  130.   return(FALSE);
  131. }
  132.  
  133.  
  134. /* Make sure argument is of mode immediate
  135.  * If not, make it #0 and signal error
  136.  */
  137. isimm(argp)
  138.   register struct astruct *argp;
  139. {
  140.   if(argp->type == T_IMM)
  141.     return(TRUE);
  142.   
  143.   sgnerr("Must be value, not %s", modename(argp));
  144.   
  145.   /* Make it #0 */
  146.   argp->type = T_IMM;
  147.   argp->disp1= 0;
  148.   argp->pres = 0;
  149.   return(FALSE);
  150. }
  151.  
  152.  
  153. /* Generate jump offset */
  154. gendisp(disp, dispdiff, op3, op4)
  155.   long disp;
  156.   int dispdiff, op3, op4;
  157. {
  158.   register int qsav;
  159.   
  160.   /* Check maximum range */
  161.   if((disp < -(long) 0x8000 || disp > (long) 0x7fff))
  162.     {
  163.       sgnwrn("Displacement of %lx out of 16-bit range", disp);
  164.       disp = 0L;
  165.     }
  166.   
  167.   /* Will it fit in 3 nibbles? */
  168.   if(disp >= -0x800 && disp <= 0x7ff)
  169.     {
  170.       /* Sure, output opcode and offset */
  171.       code1(op3);
  172.       code3(disp); 
  173.       return;
  174.     }
  175.   
  176.   /* No, use four nibbles */
  177.   disp += dispdiff;
  178.  
  179.   code2(op4);
  180.   code4(disp);
  181. }
  182.  
  183.  
  184. /* Reverse bits in byte */
  185. revbits(mask)
  186.   unsigned int mask;
  187. {
  188.   static unsigned int i;
  189.   register unsigned int mask1,mask2;
  190.   
  191.   for(mask1 = mask, i = mask2 = 0; i<8; i++)
  192.     {
  193.       mask2 <<= 1;
  194.       mask2 |= (mask1 & 1);
  195.       mask1 >>= 1;
  196.     }
  197.   
  198.   return(mask2);
  199. }
  200.